package com.sanxiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sanxiang.entity.OrderTable;
import com.sanxiang.entity.UserTable;
import com.sanxiang.entity.UserUpdate;
import com.sanxiang.mapper.OrderTableMapper;
import com.sanxiang.mapper.UserTableMapper;
import com.sanxiang.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserTableMapper userTableMapper;
    @Resource
    private OrderTableMapper orderTableMapper;

    @Override
    public UserTable selectByUserId(Integer user_id) {
        QueryWrapper<UserTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user_id);
        return userTableMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer UpdateUserTableByOrderId(UserUpdate userUpdate, String car_file) throws ParseException {
        //根据orderId查询对应的userId
        QueryWrapper<OrderTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", userUpdate.getOrderId());
        OrderTable order = orderTableMapper.selectOne(queryWrapper);
        QueryWrapper<UserTable> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", order.getOrderUserId());
        UserTable user = userTableMapper.selectOne(queryWrapper2);
        //更新数据
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        user.setUserCarNo(userUpdate.getCarNo());
        user.setUserCarModel(userUpdate.getCarModel());
        user.setUserCarPurchaseTime(ft.parse(userUpdate.getCarPurchaseTime()));
        user.setUserCarValue(userUpdate.getCarValue());
        user.setUserCarFile(car_file);
        userTableMapper.updateById(user);
        return user.getUserId();
    }
}
